package org.hotDating.android.individual.mmi;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableLayout.LayoutParams;
import org.hotDating.android.individual.mmi.R;
import org.hotDating.android.individual.mmi.Friends;

public class Frame {
    public static final int ID_MAP = 0;
    public static final int ID_FRIEND = 1;
    public static final int ID_LOCATION = 2;
    public static final int ID_DETAIL = 3;
    public static final int ID_PROGRESS = 4;

    private static ImageButton iconMap;
    private static ImageButton iconFriend;
    private static ImageButton iconLocation;
    private static ImageButton iconDetail;
    private static ImageButton iconProgress;

    public static Activity currentActivity;

    /**
     * 
     * @param a
     */
    public static void setup(Activity a){
        currentActivity = a;
        
        iconMap = (ImageButton) a.findViewById(ID_MAP);
        iconMap.setOnFocusChangeListener(iconFocusListener);
        iconMap.setOnClickListener(mapClickListener);
        iconMap.setOnTouchListener(mapTouchListener);

        iconFriend = (ImageButton) a.findViewById(ID_FRIEND);
        iconFriend.setOnFocusChangeListener(iconFocusListener);
        iconFriend.setOnClickListener(friendClickListener);
        iconFriend.setOnTouchListener(friendsTouchListener);

        iconLocation = (ImageButton) a.findViewById(ID_LOCATION);
        iconLocation.setOnFocusChangeListener(iconFocusListener);
        iconLocation.setOnClickListener(locationClickListener);

        iconDetail = (ImageButton) a.findViewById(ID_DETAIL);
        iconDetail.setOnFocusChangeListener(iconFocusListener);
        iconDetail.setOnClickListener(detailClickListener);
        
        
        iconProgress = (ImageButton) a.findViewById(ID_PROGRESS);
        iconProgress.setOnFocusChangeListener(iconFocusListener);
        iconProgress.setOnClickListener(progressClickListener);

        String selected = currentActivity.getLocalClassName();


        if(selected.compareTo("DatingMap") == 0 ) {
            iconMap.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.map_select));
//            iconMap.setBackgroundResource(R.drawable.background2);
        } else {
            iconMap.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.map));

        }

        if(selected.compareTo("Friends") == 0) {
            iconFriend.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.friends_select));
//            iconFriend.setBackgroundResource(R.drawable.background2);
        } else {
            iconFriend.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.friends));

        }

        if(selected.compareTo("Location") == 0) {
            iconLocation.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.location_foucs));
//            iconLocation.setBackgroundResource(R.drawable.background2);
        } else {
            iconLocation.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.location));

        }

        if(selected.compareTo("Detail") == 0) {
            iconDetail.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.detail_foucs));
//            iconDetail.setBackgroundResource(R.drawable.background2);
        } else {
            iconDetail.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.detail));

        }
        
        if(selected.compareTo("Progress") == 0) {
            iconProgress.setImageDrawable(a.getResources()
        	    .getDrawable(R.drawable.progress_foucs));
//            iconProgress.setBackgroundResource(R.drawable.background2);
        } else {
            iconProgress.setImageDrawable(a.getResources()
        	    .getDrawable(R.drawable.progress));
        }

    }

    private static ImageButton.OnTouchListener mapTouchListener = new ImageButton.OnTouchListener(){
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			iconMap.setImageResource(R.drawable.map_foucs);
			return false;
		}
    };
    
    private static ImageButton.OnTouchListener friendsTouchListener = new ImageButton.OnTouchListener(){
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			iconFriend.setImageResource(R.drawable.friends_foucs);
			return false;
		}
    };
    
    /**
     * 
     */
    private static ImageButton.OnClickListener mapClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Frame.currentActivity, DatingMap.class);
            Frame.currentActivity.startActivity(i);
            Frame.currentActivity.finish();
        }
    };

    
    /**
     * 
     */
    private static ImageButton.OnClickListener friendClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Frame.currentActivity, Friends.class);
            Frame.currentActivity.startActivity(i);
            Frame.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener locationClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Frame.currentActivity, Friends.class);
            Frame.currentActivity.startActivity(i);
            Frame.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener detailClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Frame.currentActivity, Friends.class);
            Frame.currentActivity.startActivity(i);
            Frame.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener progressClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Frame.currentActivity, Friends.class);
            Frame.currentActivity.startActivity(i);
            Frame.currentActivity.finish();
        }
    };


    /**
     * 
     */
     private static ImageButton.OnFocusChangeListener iconFocusListener =
         new ImageButton.OnFocusChangeListener() {
    	 

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
//			if(hasFocus) {
//                v.setBackgroundResource(R.drawable.background2);
//            } else {
//                v.setBackgroundResource(R.drawable.background);
//            }
			
		}

		
     };

     /**
      * 
      * @author Steven Osborn - http://steven.bitsetters.com
      */
     public static class FrameView extends TableLayout {

		public FrameView(Context context, AttributeSet attrs) {
			super(context, attrs);
			// Create row container
          TableRow row = new TableRow(context);

          // Create buttons
          ImageButton installButton = new ImageButton(context);
          ImageButton uninstallButton = new ImageButton(context);
          ImageButton upgradeButton = new ImageButton(context);
          ImageButton repoButton = new ImageButton(context);
          ImageButton newsButton = new ImageButton(context);

          // Give buttons UID's (sorta)
          installButton.setId(ID_MAP);
          uninstallButton.setId(ID_FRIEND);
          upgradeButton.setId(ID_LOCATION);
          repoButton.setId(ID_DETAIL);
          newsButton.setId(ID_PROGRESS);

          // Set button images
          installButton.setImageDrawable(getResources()
                  .getDrawable(R.drawable.install22));
          uninstallButton.setImageDrawable(getResources()
                  .getDrawable(R.drawable.uninstall22));
          upgradeButton.setImageDrawable(getResources()
                  .getDrawable(R.drawable.upgrade22));
          repoButton.setImageDrawable(getResources()
                  .getDrawable(R.drawable.repos22));
          newsButton.setImageDrawable(getResources()
     	     .getDrawable(R.drawable.news22));

          // Set Padding l,t,r,b
          installButton.setPadding(0, 3, 0, 3);
          uninstallButton.setPadding(0, 3, 0, 3);
          upgradeButton.setPadding(0, 3, 0, 3);
          repoButton.setPadding(0, 3, 0, 3);
          newsButton.setPadding(0, 3, 0, 3);


          // Set Background Image
          installButton.setBackgroundDrawable(null);
          uninstallButton.setBackgroundDrawable(null);
          upgradeButton.setBackgroundDrawable(null);
          repoButton.setBackgroundDrawable(null);
          newsButton.setBackgroundDrawable(null);
//          installButton.setBackgroundDrawable(getResources()
//                  .getDrawable(R.drawable.background));
//          uninstallButton.setBackgroundDrawable(getResources()
//                  .getDrawable(R.drawable.background));
//          upgradeButton.setBackgroundDrawable(getResources()
//                  .getDrawable(R.drawable.background));
//          repoButton.setBackgroundDrawable(getResources()
//                  .getDrawable(R.drawable.background));
//          newsButton.setBackgroundDrawable(getResources()
//                  .getDrawable(R.drawable.background));


          // Add buttons to row

          row.addView(installButton, new TableRow.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          row.addView(uninstallButton, new TableRow.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          row.addView(upgradeButton, new TableRow.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          row.addView(repoButton, new TableRow.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          row.addView(newsButton, new TableRow.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

          
          // Make columns expand to fill space
          setColumnStretchable(0, true);
          setColumnStretchable(1, true);
          setColumnStretchable(2, true);
          setColumnStretchable(3, true);
          setColumnStretchable(4, true);

          // Set row background color
//          row.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));

          // Add row to table
          addView(row, new TableLayout.LayoutParams(
                  LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
      }
		}
    	 
}
